package com.code.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.code.mapper.ItemsMapper;
import com.code.domain.Items;
import com.code.service.ItemsService;

@Service
@Slf4j
public class ItemsServiceImpl extends ServiceImpl<ItemsMapper, Items> implements ItemsService {

    @Autowired
    private ItemsMapper itemsMapper;

    /**
     * 查看热门或者推荐商品
     * type 0 热门商品 查询销售量最多的前4个
     * type 1 推荐商品
     *
     * @param city
     * @param type
     * @return
     */
    @Override
    public List<Items> findHotProductOrRecommendation(String city, Integer type) {
        log.info("查询{}城市的{}商品", city, type == 0 ? "热门" : "推荐");
        List<Items> itemsList = itemsMapper.selectList(new LambdaQueryWrapper<Items>()
                .eq(Items::getCity, city)
                .orderByDesc(type == 0, Items::getSales)
                .orderByDesc(type == 1, Items::getRecommendationSort)
                .last(" limit 4 ")
        );
        return itemsList;
    }
}
